import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: JSON.parse(localStorage.getItem('userInfo')) || null
  }),

  getters: {
    isLogin: (state) => !!state.token,
    username: (state) => state.userInfo?.username
  },

  actions: {
    // 登录
    login(data) {
      // 这里应该调用登录API
      this.token = 'mock_token'
      this.userInfo = {
        id: 1,
        username: data.username,
        avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
      }
      localStorage.setItem('token', this.token)
      localStorage.setItem('userInfo', JSON.stringify(this.userInfo))
    },

    // 退出登录
    logout() {
      this.token = ''
      this.userInfo = null
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
    },

    // 更新用户信息
    updateUserInfo(info) {
      this.userInfo = {
        ...this.userInfo,
        ...info
      }
      localStorage.setItem('userInfo', JSON.stringify(this.userInfo))
    }
  }
}) 